/*
 * @(#)InitiatingGatewayRetrieveWebService.java
 * Date 2013-10-19
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.xca.ws;

import ihe.iti.xds_b._2007.InitiatingGatewayRetrievePortType;
import ihe.iti.xds_b._2007.RetrieveDocumentSetRequestType;
import ihe.iti.xds_b._2007.RetrieveDocumentSetResponseType;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.jws.HandlerChain;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.soap.Addressing;
import javax.xml.ws.soap.MTOM;
import javax.xml.ws.soap.SOAPBinding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.hornerscorners.lamppost.atna.client.utils.AtnaUtils;
import us.hornerscorners.lamppost.config.IServiceConfig;
import us.hornerscorners.lamppost.exception.ServiceActionError;
import us.hornerscorners.lamppost.exception.SeverityType;
import us.hornerscorners.lamppost.ihecommon.config.IHEConfig;
import us.hornerscorners.lamppost.ihecommon.config.IHEServiceType;
import us.hornerscorners.lamppost.model.audit.AuditContext;
import us.hornerscorners.lamppost.model.audit.CodedValueType;
import us.hornerscorners.lamppost.registry.constant.ErrorCode;
import us.hornerscorners.lamppost.registry.constant.ResponseStatus;
import us.hornerscorners.lamppost.registry.model.RegistryResponse;
import us.hornerscorners.lamppost.registry.transformer.xds.RegistryResponseTypeTransformer;
import us.hornerscorners.lamppost.repository.inject.RepositoryTarget;
import us.hornerscorners.lamppost.ws.SOAPContextParser;
import us.hornerscorners.lamppost.xca.service.InitiatingRetrieveService;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-10-19
 * @author         Jim Horner    
 */
@WebService(
    portName = "InitiatingGatewayRetrieve_Port_Soap12",
    serviceName = "InitiatingGatewayRetrieve_Service",
    targetNamespace = "urn:ihe:iti:xds-b:2007",
    endpointInterface = "ihe.iti.xds_b._2007.InitiatingGatewayRetrievePortType",
    wsdlLocation = "WEB-INF/wsdl/xca/XCAInitiatingGatewayRetrieve.wsdl"
)
@MTOM(enabled = true)
@HandlerChain(file = "META-INF/xca-handler-chain.xml")
@Addressing(enabled = true, required = true)
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public class InitiatingGatewayRetrieveWebService
        implements InitiatingGatewayRetrievePortType {

    /** Field description */
    @Inject
    private IHEConfig config;

    /** Field description */
    @Resource
    private WebServiceContext context;

    /** Field description */
    @Inject
    private SOAPContextParser contextParser;
    
    /** Field description */
    private final Logger logger = LoggerFactory.getLogger(getClass());

    /** Field description */
    @Inject
    @RepositoryTarget
    private RegistryResponseTypeTransformer responseTypeTransformer;

    /** Field description */
    @Inject
    private InitiatingRetrieveService retrieveService;    
    
    /**
     * Method description
     *
     *
     * @param body
     *
     * @return
     */
    @Override
    public RetrieveDocumentSetResponseType documentRepositoryRetrieveDocumentSet(
            RetrieveDocumentSetRequestType body) {
        
        RetrieveDocumentSetResponseType result = null;

        try {
            logger.debug("XCA Initiating - Retrieve Document Set.b started");

            CodedValueType eventType = AtnaUtils.createCodedValue("ITI-39",
                                           "IHE Transactions", "Cross Gateway Retrieve");

            IServiceConfig serviceConfig =
                this.config.findServiceConfig(IHEServiceType.XCA);

            AuditContext auditContext = this.contextParser.parseAuditContext(context,
                                            eventType, serviceConfig);
            
            result = this.retrieveService.retrieveDocument(auditContext, body);

        } catch (Exception e) {

            // create a response
            logger.error(e.getMessage(), e);

            ServiceActionError error =
                new ServiceActionError(ErrorCode.XDSRepositoryError.getCode(),
                                       e.getMessage(), SeverityType.Error);

            RegistryResponse response = new RegistryResponse(ResponseStatus.Failure,
                                            getClass(), error);

            result = new RetrieveDocumentSetResponseType();
            result.setRegistryResponse(this.responseTypeTransformer.transform(response));
        }

        return result;    
    }
}
